#include "common.h"
namespace common {
void setZeroesBase(std::vector<std::vector<int>> &matrix) {
    int rows = matrix.size();
    int cols = matrix[0].size();
    std::vector<std::pair<int, int>> locs;
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            if (matrix[r][c] == 0) {
                locs.push_back({r, c});
            }
        }
    }
    for (auto loc : locs) {
        int r = loc.first;
        int c = loc.second;
        for (int i = 0; i < cols; i++) {
            matrix[r][i] = 0;
        }
        for (int j = 0; j < rows; j++) {
            matrix[j][c] = 0;
        }
    }
}
void setZeroes(std::vector<std::vector<int>> &matrix) {
    bool setCol = false, setRow = false;
    int rows = matrix.size();
    int cols = matrix[0].size();
    // 记录首列是否应该被置0
    for (int r = 0; r < rows; r++) {
        if (matrix[r][0] == 0) {
            setCol = true;
            break;
        }
    }
    // 记录行是否应该被置0
    for (int c = 0; c < cols; c++) {
        if (matrix[0][c] == 0) {

            setRow = true;
            break;
        }
    }
    // 非首行首列的置零信息存储在首行首列
    for (int r = 1; r < rows; r++) {
        for (int c = 1; c < cols; c++) {
            if (matrix[r][c] == 0) {
                matrix[0][c] = 0;
                matrix[r][0] = 0;
            }
        }
    }
    // 首行标记的位置中列置0
    for (int c = 1; c < cols; c++) {
        if (matrix[0][c] == 0) {
            for (int r = 0; r < rows; r++) {
                matrix[r][c] = 0;
            }
        }
    }

    // 首列标记的位置中列置0
    for (int r = 1; r < rows; r++) {
        if (matrix[r][0] == 0) {
            for (int c = 1; c < cols; c++) {
                matrix[r][c] = 0;
            }
        }
    }
    if (setRow) {
        for (int c = 0; c < cols; c++)
            matrix[0][c] = 0;
    }
    if (setCol) {
        for (int r = 0; r < rows; r++) {
            matrix[r][0] = 0;
        }
    }
}
} // namespace common
